/*
 * AisleNav Android Application
 * Accelerometer.java
 * project.AisleNav.Backend
 * 
 * CS495
 * 
 * Team:
 * 		Kirby Green
 * 		Mark Hale
 * 		Adam Warnock
 * 
 * Time: 6:14:34 PM
 * Date: Dec 4, 2011
 * 
 */
package project.AisleNav.Backend;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

// TODO: Auto-generated Javadoc
/**
 * Accelerometer.java Finds distance and direction user. Currently set by a file
 * since the hardware accelerometer blows.
 * 
 * @author Kirby Green
 * @version 0.1
 * 
 */
public class Accelerometer {

	/** Accelerometer data buffer list. */
	private List<AccelerometerSet> m_data = new ArrayList<AccelerometerSet>();

	/**
	 * Individual set of Accelerometer data.
	 * 
	 * @author Kirby Green
	 * @version 0.1
	 */
	private class AccelerometerSet {
		
		/** The m_time. */
		private int m_time;
		
		/** The m_distance. */
		private int m_distance;
		
		/** The m_direction. */
		private int m_direction;

		/**
		 * Default constructor. Time, distance, and direction are set to -1.
		 */
		@SuppressWarnings("unused")
		public AccelerometerSet() {
			m_time = -1;
			m_distance = -1;
			m_direction = -1;
		}

		/**
		 * Overloaded constructor with data.
		 * 
		 * @param time
		 *            Time of reading
		 * @param distance
		 *            Distance from reading
		 * @param direction
		 *            Direction from reading
		 */
		public AccelerometerSet(int time, int distance, int direction) {
			m_time = time;
			m_distance = distance;
			m_direction = direction;
		}

		/**
		 * Clear the set.
		 */
		@SuppressWarnings("unused")
		public void clear() {
			m_time = -1;
			m_distance = -1;
			m_direction = -1;
		}

		/**
		 * Set the data.
		 *
		 * @param time Time of reading
		 * @param distance Distance from reading.
		 * @param direction Direction from reading.
		 */
		@SuppressWarnings("unused")
		public void set(int time, int distance, int direction) {
			m_time = time;
			m_distance = distance;
			m_direction = direction;
		}

		/**
		 * Get the time.
		 * 
		 * @return Time of reading.
		 */
		public int getTime() {
			return m_time;
		}

		/**
		 * Get the distance.
		 * 
		 * @return Distance from reading.
		 */
		public int getDistance() {
			return m_distance;
		}

		/**
		 * Get the direction.
		 * 
		 * @return Direction from reading.
		 */
		public int getDirection() {
			return m_direction;
		}

	}

	/**
	 * Get and remove the last data set from the Accelerometer buffer.
	 * 
	 * @return Integer array of data [time,distance,direction]
	 */
	public int[] popLast() {
		AccelerometerSet as;
		int[] r = new int[3];
		if (m_data.size() > 0) {
			as = m_data.get(m_data.size() - 1);
			r[0] = as.getTime();
			r[1] = as.getDistance();
			r[2] = as.getDirection();
			m_data.remove(m_data.size() - 1);
		} else {
			r[0] = -1;
			r[1] = -1;
			r[2] = -1;			
		}
		return r;
	}
	
	/**
	 * Get the last data set from the Accelerometer buffer without removing it.
	 * 
	 * @return Integer array of data [time,distance,direction]
	 */
	public int[] getLast() {
		AccelerometerSet as;
		int[] r = new int[3];
		if (m_data.size() > 0) {
			as = m_data.get(m_data.size() - 1);
			r[0] = as.getTime();
			r[1] = as.getDistance();
			r[2] = as.getDirection();
		} else {
			r[0] = -1;
			r[1] = -1;
			r[2] = -1;
		}
		return r;
	}

	/**
	 * Get and remove the first data set from Accelerometer buffer.
	 * 
	 * @return Integer array of data [time,distance,direction]
	 */
	public int[] popFirst() {
		AccelerometerSet as;
		int[] r = new int[3];
		if (m_data.size() > 0) {
			as = m_data.get(0);
			r[0] = as.getTime();
			r[1] = as.getDistance();
			r[2] = as.getDirection();
			m_data.remove(0);
		} else {
			r[0] = -1;
			r[1] = -1;
			r[2] = -1;
		}
		return r;
	}
	/**
	 * Get the first data set from Accelerometer buffer with out removing it.
	 * 
	 * @return Integer array of data [time,distance,direction]
	 */
	public int[] getFirst() {
		AccelerometerSet as;
		int[] r = new int[3];
		if(m_data.size() > 0) {
			as = m_data.get(0);
			r[0] = as.getTime();
			r[1] = as.getDistance();
			r[2] = as.getDirection();			
		} else {
			r[0] = -1;
			r[1] = -1;
			r[2] = -1;
		}
		return r;
	}
	
	public int size() {
		return m_data.size();
	}

	/**
	 * Read in a CSV file a Accelerometer data to be placed in the buffer.
	 */
	public void readCSV() {
		System.out.println("Reading Accelerometer CSV file...");
		InputStream is;
		File dir = Environment.getExternalStorageDirectory();
		try {
			File f = new File(dir, "Accelerometer_data.csv");
			is = new FileInputStream(f);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			String line;
			try {
				while ((line = reader.readLine()) != null) {
					int time = -1;
					int dist = -1;
					int direc = -1;
					String[] RowData = line.split(",");
					
					try {
						time = Integer.parseInt(RowData[0]);
						dist = Integer.parseInt(RowData[1]);
						direc = Integer.parseInt(RowData[2]);
					} catch(NumberFormatException e) {
						System.err.println("Accelerometer readCSV - Error Formatting parseInt" + e.getMessage());
						e.printStackTrace();
					}
					
					m_data.add(new AccelerometerSet(time, dist, direc));

				}
				reader.close();
				is.close();
			} catch (IOException e) {
				System.err.println("Accelerometer readCSV - IOException");
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			System.err.println("Accelerometer readCSV - FileNotFoundException");
			e.printStackTrace();
		}

		System.out.println("DONE");
		System.out.println("Accelerometer size: " + m_data.size());
		System.out.println("First: " + m_data.get(0).getTime());
		System.out.println("Last: " + m_data.get(m_data.size() - 1).getTime());
	}

}
